﻿using OpenCvSharp;

using Xc.DataAccess.CodeReader.Properties;
using Xc.DataAccess.Core;

namespace Xc.DataAccess.CodeReader.Models.Filters
{
    /// <summary>
    /// 双边滤波
    /// </summary>
    [Filter("双边滤波", FilterType.Algorithm)]
    public class AlgorithmBilateralFilter : FilterBase
    {
        [InputParameter("大小")]
        public ApertureSize Size { get; set; } = ApertureSize.Size3;
        [InputParameter("D")]
        public int D { get; set; }
        [InputParameter("SigmaColor")]
        public double SigmaColor { get; set; }
        [InputParameter("SigmaSpace")]
        public double SigmaSpace { get; set; }
        [InputParameter("类型")]
        public BorderTypes BorderTypes { get; set; } = BorderTypes.Default;
        internal override ResultData<Mat> UseChannel(Mat mat, FilterBase[] filterInfos, FilterBase preFilterBase)
        {
            Cv2.BilateralFilter(mat, mat,D,SigmaColor,SigmaSpace, BorderTypes);
            return ResultData<Mat>.Accept(TextResource.filter_action_complete, mat);
        }
    }
    ///// <summary>
    ///// 频谱分析
    ///// </summary>
    //[Filter("频谱分析", FilterType.Algorithm)]
    //public class AlgorithmSpectralAnalysis:FilterBase
    //{
    //    [InputParameter("边框类型")]
    //    public BorderTypes BorderTypes { get; set; } = BorderTypes.Constant;
    //    /// <summary>
    //    /// 颜色
    //    /// </summary>
    //    [InputParameter("颜色")]
    //    public Scalar Scalar { get; set; } = Scalar.All(0);
    //    internal override ResultData<Mat> UseChannel(Mat mat, FilterBase[] filterInfos, FilterBase preFilterBase)
    //    {
    //        int m = Cv2.GetOptimalDFTSize(mat.Rows);
    //        int n = Cv2.GetOptimalDFTSize(mat.Cols);
    //        Mat padded = new Mat();
    //        Cv2.CopyMakeBorder(mat,padded,0,m-mat.Rows,0,n-mat.Cols, BorderTypes, Scalar);
    //        var planses = new Mat[] {new Mat<float>(padded), Mat.Zeros(padded.Size(), MatType.CV_32F) };
    //        Mat commplexI=new Mat();
    //        Cv2.Merge(planses,commplexI);
    //        Cv2.Dft(commplexI,commplexI);
    //        Cv2.Split(commplexI,out planses);
    //        Cv2.Magnitude(planses[0], planses[1], planses[0]);

    //        Mat magnitudeImage = planses[0];
    //        magnitudeImage += Scalar.All(1);
    //        Cv2.Log(magnitudeImage, magnitudeImage);
    //        magnitudeImage = magnitudeImage[0, 0, magnitudeImage.Cols & -2, magnitudeImage.Rows & -2];
    //        int cx = magnitudeImage.Cols / 2;
    //        int cy = magnitudeImage.Rows / 2;
    //        Mat q0 = new Mat(magnitudeImage, new Rect(0, 0, cx, cy));
    //        Mat q1 = new Mat(magnitudeImage, new Rect(cx, 0, cx, cy));
    //        Mat q2 = new Mat(magnitudeImage, new Rect(0, cy, cx, cy));
    //        Mat q3 = new Mat(magnitudeImage, new Rect(cx, cy, cx, cy));
    //        Mat temp = new Mat();
    //        q0.CopyTo(temp);
    //        q3.CopyTo(q0);
    //        temp.CopyTo(q3);
    //        q1.CopyTo(temp);
    //        q2.CopyTo(q1);
    //        temp.CopyTo(q2);
    //        Cv2.Normalize(magnitudeImage,magnitudeImage,0,1,NormTypes.MinMax);
    //        return ResultData<Mat>.Accept(TextResource.filter_action_complete, magnitudeImage);
    //    }
    //}
    ///// <summary>
    ///// 亮度调节
    ///// </summary>
    //[Filter("亮度调节", FilterType.Algorithm)]
    //public class AlgorithmBrightness : FilterBase
    //{
    //    /// <summary>
    //    /// 亮度
    //    /// </summary>
    //    [InputParameter("亮度")]
    //    public double Brightness { get; set; } = 1;
    //    internal override ResultData<Mat> UseChannel(Mat mat, FilterBase[] filterInfos, FilterBase preFilterBase)
    //    {
    //        Modify(mat, Brightness);
    //        return ResultData<Mat>.Accept(TextResource.filter_action_complete, mat);
    //    }
    //    /// <summary>
    //    /// 修改亮度值和对比度
    //    /// </summary>
    //    /// <param name="mat"></param>
    //    /// <param name="brightness"></param>
    //    private void Modify(Mat mat,double brightness)
    //    {
    //        for (int y = 0; y < mat.Rows; y++)
    //        {
    //            for (int x = 0; x < mat.Cols; x++)
    //            {
    //                for (int c = 0; c < 3; c++)
    //                {
    //                    var f = mat.At<Vec3d>(y, x)[c];
    //                    var d = (f + brightness);
    //                    mat.At<Vec3d>(y, x)[c]= d;
    //                }
    //            }
    //        }
    //    }
    //}
}
